﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.Post;
using FisheryPlatform.Bussiness.Option;

namespace FisheryPlatform.Web.WWW.Article
{
    public partial class Index : System.Web.UI.Page
    {
        protected List<PostsEntity> ArticleList = null;
        protected int pageIndex = 0;
        protected int pageSize = 30;
        protected int pageCount = 0;
        protected List<PostsEntity> TopList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            #region
            if (!int.TryParse(Request.QueryString["pageIndex"], out pageIndex))
            {
                pageIndex = 1;
            }

            var topIds = OptionSingleton.Instance.Get(OptionNameConstants.Posts_Top).Value;
            if (!string.IsNullOrEmpty(topIds))
            {
                var sqlWhere = " PostId in(" + topIds.Trim(',') + ")";
                TopList = PostManager.Search(sqlWhere, 0, 0, out pageCount);
            }
            else
            {
                TopList = new List<PostsEntity>();
            }

            #endregion
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            #region
            var sqlWhereString = "";
            GetSearchCondition(out sqlWhereString);
            ArticleList = PostManager.Search(sqlWhereString, pageIndex, pageSize, out pageCount);

            this.pb_PageBar.PbModel.CurrentIndex = pageIndex;
            this.pb_PageBar.PbModel.PageCount = pageCount;
            this.pb_PageBar.PbModel.PageSize = pageSize;
            this.pb_PageBar.PbModel.ChangePageUrl = "?" + this.pb_PageBar.PbModel.GetUrlParams(Request);
            this.pb_PageBar.PbModel.IsForAjax = true;
            this.pb_PageBar.PbModel.AjaxPageClickCallback = "PageChanged({index})";
            #endregion
        }

        private void GetSearchCondition(out string sqlWhereString)
        {
            #region
            sqlWhereString = " status=" + ((int)PostStatusEnum.Publish).ToString();

            if (string.IsNullOrEmpty(Request.QueryString["cate"]) == false && Request.QueryString["cate"] != "0")
            {
                sqlWhereString += " and type=" + Request.QueryString["t"];
            }

            #endregion
        }
    }
}